package com.iw.sms.quartz;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

public class SmsConfig {

	public static String AUTO_SMS_RUN = "auto.sms.run";
	public static String AUTO_REPLY_RUN = "auto.reply.run";
	public static String AUTO_RECIVE_RUN = "auto.recive.run";
	private static String configPath = "/sms-server-config.properties";

	public static boolean getRun(String key) {
		InputStream in = SmsConfig.class.getResourceAsStream(configPath);
		Properties p = new Properties();
		try {
			p.load(in);
			in.close();
			return p.getProperty(key).trim().equalsIgnoreCase("true");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public static String getProperty(String key) {
		InputStream in = SmsConfig.class.getResourceAsStream(configPath);
		Properties p = new Properties();
		try {
			p.load(in);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return p.getProperty(key);
	}

	public static void setProperty(String key, Object value) {
		InputStream in = SmsConfig.class.getResourceAsStream(configPath);
		Properties p = new Properties();
		try {
			p.load(in);
			p.setProperty(key, String.valueOf(value));
			in.close();
			try {
				OutputStream out = new FileOutputStream(SmsConfig.class.getResource(configPath).getPath());
				p.store(out, "set new property");
				out.flush();
				out.close();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
